package com.lin.blog.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.blog.entity.Tags;
import com.lin.blog.mapper.TagsMapper;
import com.lin.blog.service.ITagsService;
import com.lin.constant.ResultCode;
import com.lin.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lin
 * @since 2020-03-08
 */
@Service
@CacheConfig(cacheNames = "tags")
public class TagsServiceImpl extends ServiceImpl<TagsMapper, Tags> implements ITagsService {

    @Autowired
    private TagsMapper tagsMapper;

    /**
     * 通过主键ID查询Tags
     *
     * @param id
     * @return
     */
    @Cacheable(key = "#id",unless = "#result.code!=200")
    @Override
    public Result getTags(Long id) {
        // 参数不为空
        if (id == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }

        // 查询
        Tags tags = tagsMapper.selectById(id);

        // 不存在
        if (tags == null) {
            return Result.failure(ResultCode.TAGS_NOT_EXIT);
        }

        return Result.success(tags);
    }

    /**
     * 插入一条Tags记录
     * @param tags
     * @return
     */
    @CacheEvict(allEntries = true)
    @Override
    public Result saveTags(Tags tags) {

        if (tagsMapper.insert(tags) > 0){

            return Result.success(tags);
        }
        return Result.failure(ResultCode.INSERT_ERROR);
    }

    /**
     * 通过主键ID删除Tags
     * @param id
     * @return
     */
    @CacheEvict(allEntries = true)
    @Override
    public Result removeTags(Long id) {
        // 参数不为空
        if (id == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        if (tagsMapper.deleteById(id) > 0) {
            return Result.success();
        }
        return Result.failure(ResultCode.DELETE_ERROR);
    }


    /**
     * 通过主键ID更新Tags
     * @param id
     * @param tags
     * @return
     */
    @CacheEvict(allEntries = true)
    @Override
    public Result updateTags(Long id, Tags tags) {
        // 参数不为空
        if (id == null) {
            return Result.failure(ResultCode.PARAM_IS_BLANK);
        }
        tags.setId(id);

        if (tagsMapper.updateById(tags) > 0) {
            return Result.success();
        }
        return Result.failure(ResultCode.UPDATE_ERROR);
    }


    /**
     * 获取TagsList
     * @return
     */
    @Cacheable(unless = "#result.code!=200")
    @Override
    public Result listTags() {
        return Result.success(tagsMapper.selectList(null));
    }

    /**
     * 分页获取TagsList
     * @return
     */
    @Cacheable(key = "#pageNum+'/'+#pageSize",unless = "#result.code!=200")
    @Override
    public Result listTags(Long pageNum, Long pageSize) {
        Page page = new Page(pageNum, pageSize);
        return Result.success(tagsMapper.selectPage(page, null));
    }
}
